// 封装分类
import { ref, onMounted } from "vue";
import { useRoute } from "vue-router";
import { CategoryGetGoodsList } from "@/api/category";
import { onBeforeRouteUpdate } from "vue-router";
export function useCategory() {
  const goodsList = ref([]);
  // 获取路由参数
  const route = useRoute();
  // 默认给一个id
  const getGoodsList = async (id = route.params.id) => {
    const res = await CategoryGetGoodsList(id);
    goodsList.value = res.result;
  };
  // 解决路由缓存问题
  onBeforeRouteUpdate((to) => {
    // 拿到新的id
    getGoodsList(to.params.id);
  });
  onMounted(() => {
    getGoodsList();
  });
  return {
    goodsList
  }
}
